function makeslicedisp(slpos, sdir, bx)
% makeslicedisp  - create an image with several slices from one view
%
% FORMAT:       makeslicedisp(slpos [, sdir [, bx]])
%
% Input fields:
%
%       slpos       list of slices to sample
%       sdir        direction ({1}: saggital, 2: coronal, 3: axial)
%       bx          2x2 sub-box (not the entire 256x256 VMR space, 1:256)
%
% No output fields, will be shown in new figure.

% Version:  v0.7g
% Build:    9100717
% Date:     Oct-07 2009, 5:21 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
   ~isa(slpos, 'double') || ...
    isempty(slpos) || ...
    any(isinf(slpos(:)) | isnan(slpos(:)) | slpos(:) < -128 | slpos(:) > 128)
    error( ...
        'BVQXtools:BadArgument', ...
        'Invalid argument supplied.' ...
    );
end
slpos = round(slpos(:))';
if nargin < 2 || ...
    numel(sdir) ~= 1 || ...
   ~any((1:3) == sdir)
    sdir = 1;
end
sdir = real(double(sdir));

% set correct map, etc.
global BVQXti;

% create array for screenshots
slc = cell(1, numel(slpos));

% iterate over positions
for c = 1:numel(slc)
    switch (sdir)
        case {1}
            BVQXti.fcfg.cPos(3) = slpos(c);
            BVQXtool(0, 'setslicepos');
            fr = getframe(get(BVQXti.h.SagImage, 'parent'));
        case {2}
            BVQXti.fcfg.cPos(1) = slpos(c);
            BVQXtool(0, 'setslicepos');
            fr = getframe(get(BVQXti.h.CorImage, 'parent'));
        case {3}
            BVQXti.fcfg.cPos(2) = slpos(c);
            BVQXtool(0, 'setslicepos');
            fr = getframe(get(BVQXti.h.TraImage, 'parent'));
    end
    slc{c} = fr.cdata;
end

% pack data into one slab
slc = cat(4, slc{:});

% possibly restrict data
if nargin > 2 && ...
    isa(bx, 'double') && ...
    isequal(size(bx), [2, 2]) && ...
   ~any(isinf(bx(:)) | isnan(bx(:)) | bx(:) < 1 | bx(:) > 256)
    bx = round(bx);
    slc = slc(bx(1,1):bx(2,1), bx(1,2):bx(2,2),:,:);
end

% show in a new figure
figure;
image(packmosaic(slc));
